package com.shuiying.hikari.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuiying.hikari.common.QueryPageParam;
import com.shuiying.hikari.entity.User;
import com.shuiying.hikari.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/list")
    public List<User> listUsers() {
        return userService.list();
    }

    //新增
    @PostMapping("/save")
    public boolean save(@RequestBody User user){
        return userService.save(user);
    }
    //修改
    @PostMapping("/update")
    public boolean update(@RequestBody User user){
        return userService.updateById(user);
    }
    //新增或修改
    @PostMapping("/saveOrUpdate")
    public boolean saveOrUpdate(@RequestBody User user){
        return userService.saveOrUpdate(user);
    }
    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id){
        return userService.removeById(id);
    }
    // 查询（模糊，匹配）
    @RequestMapping(value = "/listP", method = {RequestMethod.GET, RequestMethod.POST})
    public List<User> listP(@RequestBody(required = false) User user) {
        LambdaQueryChainWrapper<User> lambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(userService.getBaseMapper());
        if (user != null) {
            if (user.getId() != null) {
                lambdaQueryChainWrapper.eq(User::getId, user.getId());
            }
            if (user.getZh() != null && !user.getZh().isEmpty()) {
                lambdaQueryChainWrapper.like(User::getZh, user.getZh());
            }
            if (user.getSex() != null) {
                lambdaQueryChainWrapper.eq(User::getSex, user.getSex());
            }
            if (user.getName() != null && !user.getName().isEmpty()) {
                lambdaQueryChainWrapper.like(User::getName, user.getName());
            }
        }
        return lambdaQueryChainWrapper.list();
    }
    @PostMapping("/listPage")
    public List<User> listPage(@RequestBody QueryPageParam query) {
//        System.out.println(query);
//        System.out.println("num=="+query.getPageNum());
//        System.out.println("size=="+query.getPageSize());
//分页
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String zh = (String)param.get("zh");
        System.out.println("name=="+(String)param.get("name"));
        System.out.println("zh=="+(String)param.get("zh"));
//        return null;

        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(User::getName, name);


        IPage result = userService.page(page, lambdaQueryWrapper);
        System.out.println("total=="+result.getTotal());//总记录数
        return null;
    }

}
